package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hxf.domain.vo.AdviceVo;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 公告 信息操作处理
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController {

    private final ISysNoticeService noticeService;
    private final ISysOssService iSysOssService;

    /**
     * 获取通知公告列表
     */
//    @SaCheckPermission("system:notice:list")
    @GetMapping("/list")
    public TableDataInfo<SysNotice> list(SysNotice notice, PageQuery pageQuery) {
        TableDataInfo<SysNotice> sysNoticeTableDataInfo = noticeService.selectPageNoticeList(notice, pageQuery);
//        for (SysNotice row : sysNoticeTableDataInfo.getRows()) {
//            if(row.getNoticePhoto()!=null){
//                String[] split = row.getNoticePhoto().split(",");
//                StringBuffer stringBuffer = new StringBuffer();
//                for (String s : split) {
//                    List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(s)));
//                    for (int i = 0; i < list.size(); i++) {
//                        if (stringBuffer.length() == 0) {
//                            stringBuffer.append(list.get(i).getUrl());
//                        } else {
//                            stringBuffer.append("," + list.get(i).getUrl());
//                        }
//                    }
//                }
//                row.setNoticeUrl(stringBuffer.toString());
//                noticeService.myUpdateNotice(row);
//            }
//        }
        return sysNoticeTableDataInfo;
    }
    /**
     * 获取通知公告列表
     */
    @GetMapping("/AppList")
    public TableDataInfo<SysNotice> AppList(SysNotice notice, PageQuery pageQuery) {
        TableDataInfo<SysNotice> sysNoticeTableDataInfo = noticeService.selectPageNoticeList(notice, pageQuery);
//        for (SysNotice row : sysNoticeTableDataInfo.getRows()) {
//            if(row.getNoticePhoto()!=null){
//                String[] split = row.getNoticePhoto().split(",");
//                StringBuffer stringBuffer = new StringBuffer();
//                for (String s : split) {
//                    List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(s)));
//                    for (int i = 0; i < list.size(); i++) {
//                        if (stringBuffer.length() == 0) {
//                            stringBuffer.append(list.get(i).getUrl());
//                        } else {
//                            stringBuffer.append("," + list.get(i).getUrl());
//                        }
//                    }
//                }
//                row.setNoticeUrl(stringBuffer.toString());
//                noticeService.myUpdateNotice(row);
//            }
//        }
        return sysNoticeTableDataInfo;
    }
    /**
     * 根据通知公告编号获取详细信息
     *
     * @param noticeId 公告ID
     */
//    @SaCheckPermission("system:notice:query")
    @GetMapping(value = "/{noticeId}")
    public R<SysNotice> getInfo(@PathVariable Long noticeId) {
        SysNotice sysNotice = noticeService.selectNoticeById(noticeId);
//        if(sysNotice.getNoticePhoto()!=null){
//            String[] split = sysNotice.getNoticePhoto().split(",");
//            StringBuffer stringBuffer = new StringBuffer();
//            for (String s : split) {
//                List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(s)));
//                for (int i = 0; i < list.size(); i++) {
//                    if (stringBuffer.length() == 0) {
//                        stringBuffer.append(list.get(i).getUrl());
//                    } else {
//                        stringBuffer.append("," + list.get(i).getUrl());
//                    }
//                }
//            }
//            sysNotice.setNoticeUrl(stringBuffer.toString());
//            noticeService.myUpdateNotice(sysNotice);
//        }
        return R.ok(sysNotice);
    }

    /**
     * 新增通知公告
     */
//    @SaCheckPermission("system:notice:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody SysNotice notice) {
        if(notice.getNoticePhoto()!=null){
            String[] split = notice.getNoticePhoto().split(",");
            StringBuffer stringBuffer = new StringBuffer();
            for (String s : split) {
                List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(s)));
                for (int i = 0; i < list.size(); i++) {
                    if (stringBuffer.length() == 0) {
                        stringBuffer.append(list.get(i).getUrl());
                    } else {
                        stringBuffer.append("," + list.get(i).getUrl());
                    }
                }
            }
            notice.setNoticeUrl(stringBuffer.toString());
        }
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改通知公告
     */
//    @SaCheckPermission("system:notice:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody SysNotice notice) {
        if(notice.getNoticePhoto()!=null){
            String[] split = notice.getNoticePhoto().split(",");
            StringBuffer stringBuffer = new StringBuffer();
            for (String s : split) {
                List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(s)));
                for (int i = 0; i < list.size(); i++) {
                    if (stringBuffer.length() == 0) {
                        stringBuffer.append(list.get(i).getUrl());
                    } else {
                        stringBuffer.append("," + list.get(i).getUrl());
                    }
                }
            }
            notice.setNoticeUrl(stringBuffer.toString());
        }else {
            notice.setNoticeUrl(null);
        }
        notice.setUpdateBy(LoginHelper.getUsername());
        notice.setUpdateTime(DateUtils.getNowDate());
        return toAjax(noticeService.myUpdateNotice(notice));
    }

    /**
     * 删除通知公告
     *
     * @param noticeIds 公告ID串
     */
//    @SaCheckPermission("system:notice:remove")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public R<Void> remove(@PathVariable Long[] noticeIds) {
        return toAjax(noticeService.deleteNoticeByIds(noticeIds));
    }

    /**
     * 获取二条校园快讯最新发布的通知
     * （手机端）
     *
     * @return
     */
    @GetMapping("/SelectCurrNoticeLimit")
    public R<List<SysNotice>> SelectCurrNoticeLimit() {
        return R.ok(noticeService.SelectCurrNoticeLimit());
    }

    /**
     * 获取所以的校园快讯的消息
     * （手机端）
     *
     * @return
     */
    @GetMapping("/SelectCurrNotice")
    public R<List<SysNotice>> SelectCurrNotice() {
        return R.ok(noticeService.SelectCurrNotice());
    }
}
